﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Window
{
	/// <summary>
	/// Test wizard view
	/// </summary>
	public partial class TestWizardView
    {
        #region Parameters
        
        bool isChanged = false;
        string _lastPageId;
        string _duration;
        TestWizardViewModel _testWizardViewModel;

        #endregion

        #region Ctor

        public TestWizardView()
		{
			InitializeComponent();

            frmPage.MouseEnter += frmPage_MouseEnter;
            dtDurationTime.PropertyChanged += dtDurationTime_PropertyChanged;

            DataContextChanged += new DependencyPropertyChangedEventHandler(TestWizardView_DataContextChanged);
		}

        void TestWizardView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            _testWizardViewModel = (TestWizardViewModel)DataContext;
            _testWizardViewModel.PropertyChanged += viewModel_PropertyChanged;
        }

        #endregion

        #region Properties
        
        /// <summary>
        /// Indicator of current page initialization. 
        /// Made it public for BackButtonClick correct handling.
        /// </summary>
        public bool IsChanged
        {
            set { isChanged = value; }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handle page navigation using keys.
        /// </summary>
        /// <param name="e">Key event arguments</param>
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Space ||
                e.Key == Key.Down ||
                e.Key == Key.Right ||
                e.Key == Key.Enter ||
                e.Key == Key.Return ||
                e.Key == Key.BrowserForward ||
                e.Key == Key.PageDown)
            {
                if (_testWizardViewModel.CanMoveToNextPage)
                    _testWizardViewModel.MoveToNextPage();    
            }

            else if (e.Key == Key.Back ||
                e.Key == Key.Up ||
                e.Key == Key.Left ||
                e.Key == Key.PageUp ||
                e.Key == Key.BrowserBack)
            {
                if (_testWizardViewModel.CanMoveToPreviousPage)
                    _testWizardViewModel.MoveToPreviousPage();
            }

            base.OnPreviewKeyDown(e);
        }

        /// <summary>
        /// Update internal duration variable every second.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dtDurationTime_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Second")
                _duration = dtDurationTime.ToString();
        }

        /// <summary>
        /// Due to the fact that the Frame.SourceUpdated doesn't work 
        /// I have to make this durty hack.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse event arguments</param>
        void frmPage_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!isChanged)
            {
                _testWizardViewModel.InitializeCurentPageAnswers(this);
                _lastPageId = _testWizardViewModel.CurrentPage.Id;
                isChanged = true;
            }
        }

        /// <summary>
        /// Save last parameters and recharge the new one.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Property changed event arguments</param>
        void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentPage" )
                FreezLastState();
        }

        #endregion

        #region Public Methods

        public void FreezLastState()
        {
            _testWizardViewModel.CurrentPage.FixTime(_lastPageId, _duration);
            _duration = String.Empty;
            dtDurationTime.Restart();
            isChanged = false;
        }

        #endregion
    }
}